﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_Common_PagingLink : System.Web.UI.UserControl
{
    public string CssClass
    {
        set
        {
            divPaging.Attributes.Remove("class");
            divPaging.Attributes.Add("class", value);
        }
    }

    protected int _PageSize;
    public int PageSize
    {
        get { return _PageSize; }
        set { _PageSize = value; }
    }

    protected bool _IsPaging = true;
    public bool IsPaging
    {
        get { return _IsPaging; }
        set { _IsPaging = value; }
    }

    protected int _TotalItems;
    public int TotalItems
    {
        get { return _TotalItems; }
        set { _TotalItems = value; }
    }

    protected int _CurrentPage;
    public int CurrentPage
    {
        get { return _CurrentPage; }
        set { _CurrentPage = value; }
    }

    protected string _CategoryID;
    public string CategoryID
    {
        get { return _CategoryID; }
        set { _CategoryID = value; }
    }

    protected string _cID;
    public string cID
    {
        get { return _cID; }
        set { _cID = value; }
    }

    protected string _CategoryName;
    public string CategoryName
    {
        get { return _CategoryName; }
        set { _CategoryName = value; }
    }

    protected string _Type;
    public string Type
    {
        get { return _Type; }
        set { _Type = value; }
    }

    protected string _Url;
    public string Url
    {
        get { return _Url; }
        set { _Url = value; }
    }
    public void BeginPaging()
    {
        if (this.Visible)
        {
            if (_PageSize == 0) _PageSize = 15;
            if (_CurrentPage == 0) _CurrentPage = 1;

            rptPaging.DataSource = HNG.Utilities.Common.CommonUtils.ItemsPaging(_CurrentPage, _PageSize, _TotalItems);
            rptPaging.DataBind();
        }
    }

    public void PagingProduct(int page, int size, int totalpage)
    {
        CurrentPage = page;
        _PageSize = size;
        _TotalItems = totalpage;
        BeginPaging();
    }

    protected void rptPaging_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView dr = e.Item.DataItem as DataRowView;
        HyperLink lnkItemPaging = (HyperLink)e.Item.FindControl("lnkItemPaging");

        if (lnkItemPaging != null && dr != null)
        {
            lnkItemPaging.Text = dr["Text"].ToString();

            if (string.IsNullOrEmpty(Request.QueryString["k"]))
            {
                lnkItemPaging.NavigateUrl = HNG.LIB.Localization.LocalizationUtils.BuildUrlSearch(_Url,dr["Argument"].ToString(),Request.QueryString["k"],this.Page);
            }
            else if (!string.IsNullOrEmpty(_CategoryID) && string.IsNullOrEmpty(_cID))
            {
                lnkItemPaging.NavigateUrl = string.Format(_Url,
                                                        HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage(),
                                                        _CategoryID,
                                                        dr["Argument"].ToString());
            }
            lnkItemPaging.Enabled = bool.Parse(dr["Enable"].ToString());

            if (lnkItemPaging.Enabled)
            {
                lnkItemPaging.CssClass = "item-page";
            }
            else
            {
                lnkItemPaging.CssClass = "currentpage";
            }
        }
    }
}